package com.tongji.housediy.view.components
{
	import com.tongji.housediy.events.commonEvent.CursorBitmapEvent;
	import com.tongji.housediy.events.commonEvent.MouseEnabledAdminEvent;
	import com.tongji.housediy.events.commonEvent.RoomNameAdminEvent;
	import com.tongji.housediy.events.commonEvent.RotationRelatedEvent;
	import com.tongji.housediy.events.commonEvent.RulerAdminEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideTitleWindowEvent;
	import com.tongji.housediy.utils.AbsorptionDetection;
	import com.tongji.housediy.utils.CollisionDetection;
	import com.tongji.housediy.utils.MathCalculation;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.Mouse;
	
	import mx.core.UIComponent;

	/**
	 * 这个类用来绘制 顶点的圆形，继承自 RoomShape
	 * @author chenchao
	 * 
	 */	
	public class RoomVertex extends RoomShape
	{
		public function RoomVertex(data:Vector.<Number>, index:int = -1)
		{
			super(data, index);
			draw();
		}
		
		/**
		 * Expand Room 的时候需要重置该顶点坐标
		 * @param x 需要重置顶点的 X 轴坐标
		 * @param y 需要重置顶点的 Y 轴坐标
		 * 
		 */		
		public function reDraw(x:Number, y:Number):void
		{
			data[0] = x;
			data[1] = y;
			draw();
		}
		
		override protected function draw():void
		{
			this.graphics.clear();
			
			this.graphics.lineStyle(1, 0x000000);
			this.graphics.beginFill(0xffffff);
			this.graphics.drawCircle(data[0], data[1], 5);
			this.graphics.endFill();
		}
		
		override protected function mouseOverHandler(event:MouseEvent):void
		{
			super.mouseOverHandler(event);
			
			// 更改光标
			if (this.parent is IndependentFloor || this.parent is InteriorWall)
				dispatchEvent(new CursorBitmapEvent(CursorBitmapEvent.SHOW_CUSTOM_CURSOR, this.parent.parent.parent.x + this.parent.x + data[0], this.parent.parent.parent.y + this.parent.y + data[1], "center"));
			else
				dispatchEvent(new CursorBitmapEvent(CursorBitmapEvent.SHOW_CUSTOM_CURSOR, this.parent.x + data[0], this.parent.y + data[1], "center"));
		}
		
		override protected function mouseOutHandler(event:MouseEvent):void
		{
			super.mouseOutHandler(event);
			
			// 移除光标
			dispatchEvent(new CursorBitmapEvent(CursorBitmapEvent.HIDE_CUSTOM_CURSOR, 0, 0));
		}
		
		override protected function mouseDownHandler(event:MouseEvent):void
		{
			///////////////////////by zazisb
			AbsorptionDetection.CancelAbsorption(this);
			
			super.mouseDownHandler(event);
			
			// 隐藏 InfoWindow
			this.dispatchEvent(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.HIDE_TITLE_WINDOW));
			
			// 隐藏 RoomName
			this.dispatchEvent(new RoomNameAdminEvent(RoomNameAdminEvent.HIDE_ROOM_NAME));
			
			// 移除场景中的 rotationBtn
			if (!(this.parent is InteriorWall))
			{
				this.dispatchEvent(new RotationRelatedEvent(RotationRelatedEvent.HIDE_ROTATION_BTN));
			}

			this.dispatchEvent(new RulerAdminEvent(RulerAdminEvent.HIDE_ALL_RULERS, this.parent as AbstractDrawingRoom));
			
			// 关闭 GridContainer 的 mouseChildren 属性
			this.dispatchEvent(new MouseEnabledAdminEvent(MouseEnabledAdminEvent.MOUSE_ENABLED_CLOSED));
		}
		
		override protected function enterFrameHandler(event:Event):void
		{
			super.enterFrameHandler(event);
			
			if (this.parent is IndependentFloor && (this.parent.rotation % 360) != 0)
			{
				var point1:Point = MathCalculation.calculateOffsetPoint(new Point(offsetX, offsetY), -this.parent.rotation);
				offsetX = point1.x;
				offsetY = point1.y;
			}
			
			var tempOffsetX:Number = 0;
			var tempOffsetY:Number = 0;
			
			if ((this.parent as AbstractDrawingRoom).parentGridContainer.isXKeyDown == true)
			{
				(this.parent as AbstractDrawingRoom).moveVertex(offsetX, 0, this);
				tempOffsetX = offsetX;
			}
			else if ((this.parent as AbstractDrawingRoom).parentGridContainer.isYKeyDown == true)
			{
				(this.parent as AbstractDrawingRoom).moveVertex(0, offsetY, this);
				tempOffsetY = offsetY;
			}
			else
			{
				(this.parent as AbstractDrawingRoom).moveVertex(offsetX, offsetY, this);
				tempOffsetX = offsetX;
				tempOffsetY = offsetY;
			}
			
			if (!(this.parent is IndependentFloor) && !(this.parent is InteriorWall))
			{
				if (CollisionDetection.isCollision(this)
					|| (this.parent as AbstractDrawingRoom).isBorderChildCollisionByVertex(this)
					|| MathCalculation.isRoomOverTurn((this.parent as AbstractDrawingRoom).vertices))
					(this.parent as AbstractDrawingRoom).moveVertex(-tempOffsetX, -tempOffsetY, this);
			}
			else if (this.parent is IndependentFloor || this.parent is InteriorWall)
			{
				if (CollisionDetection.isCollisionWithOther(this.parent as UIComponent))
					(this.parent as AbstractDrawingRoom).moveVertex(-tempOffsetX, -tempOffsetY, this);
			}
			
			// 更改光标
			if (this.parent is IndependentFloor || this.parent is InteriorWall)
				dispatchEvent(new CursorBitmapEvent(CursorBitmapEvent.UPDATE_CUSTOM_CURSOR, this.parent.parent.parent.x + this.parent.x + data[0], this.parent.parent.parent.y + this.parent.y + data[1]));
			else
				dispatchEvent(new CursorBitmapEvent(CursorBitmapEvent.UPDATE_CUSTOM_CURSOR, this.parent.x + data[0], this.parent.y + data[1]));
		}
		
		override protected function mouseUpHandler(event:MouseEvent):void
		{
			super.mouseUpHandler(event);
			//(this.parent as AbstractDrawingRoom).hideRuler();
			//隐藏标尺
			this.dispatchEvent(new RulerAdminEvent(RulerAdminEvent.HIDE_ALL_RULERS));
			
			// 移除光标
			dispatchEvent(new CursorBitmapEvent(CursorBitmapEvent.HIDE_CUSTOM_CURSOR, 0, 0));
			
			// 开启 GridContainer 的 mouseChildren 属性
			this.dispatchEvent(new MouseEnabledAdminEvent(MouseEnabledAdminEvent.MOUSE_ENABLED_OPENED));
			///////////////by zazisb
			AbsorptionDetection.DoAbsorption(this);
		}
	}
}